//
//  NewFeatureViewController.swift
//  WeiBo
//
//  Created by tusm on 2017/6/28.
//  Copyright © 2017年 GM. All rights reserved.
//

import UIKit

private let WBNewFeatureViewID = "NewFeatureCell"
private let WBNewFeatureImageCount = 4


class NewFeatureViewController: UICollectionViewController {
    
    
    
    init(){
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.main.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.isPagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var prefersStatusBarHidden: Bool{
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.collectionView!.register(NewFeatureCell.self, forCellWithReuseIdentifier: WBNewFeatureViewID)
        
        

        // Do any additional setup after loading the view.
    }

       // MARK: UICollectionViewDataSource

       override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return WBNewFeatureImageCount
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WBNewFeatureViewID, for: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
    
        return cell
    }
    
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int (scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page != WBNewFeatureImageCount - 1{
            return
        }
        
        let cell = collectionView?.cellForItem(at: NSIndexPath(item: page, section: 0) as IndexPath) as! NewFeatureCell
        
        
        cell.showButtonAnim()
        
        
    
    }

}

fileprivate class NewFeatureCell:UICollectionViewCell{
    
    private lazy var iconView:UIImageView = UIImageView()
    private lazy var startButton:UIButton = UIButton(title: "开始体验", imageName: "new_feature_finish_button",color:UIColor.black)
    
     var imageIndex:Int = 0{
        didSet{
            
            iconView.image = UIImage(named:"new_feature_\(imageIndex + 1)")
            
            startButton.isHidden = true
        }
    }
    
    fileprivate func showButtonAnim(){
        
       startButton.isHidden = false
       startButton.transform = CGAffineTransform(scaleX: 0,y: 0)
       startButton.isUserInteractionEnabled = false
        
       UIView.animate(withDuration: 1.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: {
        
           self.startButton.transform = CGAffineTransform.identity
        
       }) { (_) in
        
           self.startButton.isUserInteractionEnabled = true
        }
    }
    
    override init(frame:CGRect){
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        addSubview(iconView)
        addSubview(startButton)
        
        iconView.frame = bounds
        
        startButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(100)
            make.width.equalTo(150)
            make.height.equalTo(40)
        }
        
        //iconView.backgroundColor = UIColor.yellow
    }
    
    
}
